(function(){
    "use strict";
    app.controller('roleCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = ' 用户中心> 角色';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取用户列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        roleName:$scope.searchByRolename,
                    };
//                    searchParam = [];
//                if($scope.searchByRolename&&$scope.searchByRolename!=""){
//                    searchParam.push({"param":{"name":$scope.searchByRolename},"sign":"LK"});
//                }
                //params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/role/list',
                    method: 'POST',
                    data: params,
                    //noParam: true,
                    success:function(data){
                        if(data.rows && data.rows.length!=0){
                            $scope.roleList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //新增
            $scope.add = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/role/add.html',
                    controller : 'addRoleModalCtrl'// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //编辑
            $scope.update = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/role/update.html',  //指向上面创建的视图
                    controller : 'updateRoleModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    angular.extend(item,data);
                },function(){});
            };
            //删除
            $scope.remove = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/userCenter/role/delete.html',
                    controller : 'deleteRoleModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                    $scope.isCheck = false;
                },function(){});
            };
        }
    ]);

    //新增ctrl
    angular.module('app').controller('addRoleModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
    	console.log($scope.addData);
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/role/create',
                    method:'POST',
                    data: $scope.addData,
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //编辑ctrl
    angular.module('app').controller('updateRoleModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            $scope.updateData.roleId = itemData.roleId;
            $scope.updateData.roleName = itemData.roleName;
            $scope.updateData.roleDesc = itemData.roleDesc;
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/role/updateUnderRoles',
                    method:'POST',
                    data: $scope.updateData,
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除ctrl
    angular.module('app').controller('deleteRoleModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/role/delete',
                    method:'POST',
                    data:{roleId: id},
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();